package com.slipper.system.menu;

import com.slipper.common.bo.Result;
import com.slipper.common.util.StringUtil;
import com.slipper.system.common.UserDetail;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import com.slipper.system.api.menu.bo.MenuBO;
import com.slipper.system.api.menu.service.IMenuSv;
import com.slipper.system.common.Constants;
import com.slipper.common.permit.PermitAll;
import com.slipper.common.tree.Node;
import com.slipper.common.tree.TreeInfo;
import com.slipper.common.tree.TreeUtil;
import com.slipper.common.util.ResultUtil;

/**
 * 菜单信息表
 * 
 * @date 2019-10-03 14:09:46
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private IMenuSv menuSv;

    @PostMapping("/queryPage")
    public String queryPage(@RequestBody MenuBO bo) {
        return ResultUtil.toJson(menuSv.queryPage(bo));
    }
    
    @PermitAll
    @PostMapping("/queryTree")
    public String queryTree(@RequestBody MenuBO bo) {
        Result<List<MenuBO>> result = menuSv.queryList(bo);
        TreeInfo treeInfo = new TreeInfo("菜单树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.toJson(TreeUtil.buildTree(treeInfo, result.getData()));
    }

    @PostMapping("/save")
    public String save(@RequestBody MenuBO bo) {
        return ResultUtil.toJson(menuSv.save(bo));
    }

    @PostMapping("/updateParentId")
    public String updateParentId(@RequestBody MenuBO bo) {
        if (StringUtil.isBlank(bo.getParentId())) {
            bo.setParentId(Constants.ROOT_ID);
        }
        if (bo.getId().equals(bo.getParentId())) {
            return ResultUtil.toJson(ResultUtil.failureResult("不能移动到此目录！"));
        }
        
        return ResultUtil.toJson(menuSv.updateParentId(bo));
    }

    @PostMapping("/updateConfig")
    public String updateConfig(@RequestBody MenuBO bo) {
        if (StringUtil.isBlank(bo.getId())) {
            return ResultUtil.toJson(ResultUtil.failureResult("参数不正确！"));
        }

        return ResultUtil.toJson(menuSv.updateConfig(bo));
    }

    @PermitAll
    @PostMapping("/queryBy")
    public String queryBy(@RequestBody MenuBO bo) {
        return ResultUtil.toJson(menuSv.queryBy(bo.getId()));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody MenuBO bo) {
        return ResultUtil.toJson(menuSv.delete(bo));
    }
    
    @PermitAll
    @PostMapping("/queryMenuTree")
    public String queryMenuTree() {
        MenuBO menuBO = new MenuBO();
        menuBO.setEmployeeId(Objects.requireNonNull(Constants.getCurrentUser()).getEmployeeId());

        // 更新权限路径
        UserDetail userDetail = Constants.getCurrentUser();
        userDetail.setPermitSet(this.menuSv.queryPermitUrl(menuBO).getData());
        Constants.setCurrentUser(userDetail);

        return ResultUtil.toJson(buildMenuTree(this.menuSv.queryUserMenus(menuBO).getData()));
    }
    
    private Node buildMenuTree(List<MenuBO> menuList) {
        TreeInfo treeInfo = new TreeInfo("菜单树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setHrefField("vuePath");
        treeInfo.setSortField("ordinal");
        return TreeUtil.buildTree(treeInfo, menuList);
    }
}
